﻿namespace PlugNT.Pack.Imager
{
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Web;

    public class ImagePage
    {
        public static void Create()
        {
            string strName = (HttpContext.Current.Request.QueryString["str"] == null) ? "" : HttpContext.Current.Request.QueryString["str"].ToString();
            string color = (HttpContext.Current.Request.QueryString["color"] == null) ? "" : HttpContext.Current.Request.QueryString["color"].ToString();
            string font = (HttpContext.Current.Request.QueryString["font"] == null) ? "" : HttpContext.Current.Request.QueryString["font"].ToString();
            string rotate = (HttpContext.Current.Request.QueryString["rotate"] == null) ? "" : HttpContext.Current.Request.QueryString["rotate"].ToString();
            string size = (HttpContext.Current.Request.QueryString["size"] == null) ? "" : HttpContext.Current.Request.QueryString["size"].ToString();
            string x = (HttpContext.Current.Request.QueryString["x"] == null) ? "" : HttpContext.Current.Request.QueryString["x"].ToString();
            string y = (HttpContext.Current.Request.QueryString["y"] == null) ? "" : HttpContext.Current.Request.QueryString["y"].ToString();
            string img = (HttpContext.Current.Request.QueryString["img"] == null) ? "" : HttpContext.Current.Request.QueryString["img"].ToString();
            if (((((strName != "") && (color != "")) && ((rotate != "") && (size != ""))) && (((font != "") && (x != "")) && (y != ""))) && (img != ""))
            {
                Create(strName, color, font, float.Parse(rotate), int.Parse(size), int.Parse(x), int.Parse(y), img);
            }
        }

        public static void Create(string reStr, string color, string fontstr, float rotate, int size, int x, int y, string img)
        {
            string imagePath = HttpContext.Current.Server.MapPath("/") + img;
            if (File.Exists(imagePath))
            {
                Image image = Image.FromFile(imagePath);
                Graphics g = Graphics.FromImage(image);
                try
                {
                    Font font = new Font(fontstr, (float) size, FontStyle.Regular);
                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(2, 10, image.Width, image.Height), Color.FromName(color), Color.DarkRed, 1.8f, true);
                    Matrix mat = new Matrix();
                    mat.RotateAt(rotate, new PointF((float) x, (float) y));
                    g.Transform = mat;
                    g.DrawString(reStr, font, brush, (float) x, (float) y);
                    MemoryStream ms = new MemoryStream();
                    image.Save(ms, ImageFormat.Jpeg);
                    HttpContext.Current.Response.ClearContent();
                    HttpContext.Current.Response.ContentType = "image/Gif";
                    HttpContext.Current.Response.BinaryWrite(ms.ToArray());
                }
                finally
                {
                    g.Dispose();
                    image.Dispose();
                }
            }
        }

        public static string Create(string reStr, string saPath, string color, string fontstr, float rotate, int size, int x, int y, string img)
        {
            string imagePath = HttpContext.Current.Server.MapPath("/") + img;
            string savePath = "";
            if (File.Exists(imagePath))
            {
                Image image = Image.FromFile(imagePath);
                Graphics g = Graphics.FromImage(image);
                try
                {
                    Font font = new Font(fontstr, (float) size, FontStyle.Regular);
                    LinearGradientBrush brush = new LinearGradientBrush(new Rectangle(2, 10, image.Width, image.Height), Color.FromName(color), Color.DarkRed, 1.8f, true);
                    Matrix mat = new Matrix();
                    mat.RotateAt(rotate, new PointF((float) x, (float) y));
                    g.Transform = mat;
                    g.DrawString(reStr, font, brush, (float) x, (float) y);
                    savePath = HttpContext.Current.Server.MapPath("/") + saPath;
                    image.Save(savePath, ImageFormat.Jpeg);
                }
                finally
                {
                    g.Dispose();
                    image.Dispose();
                }
            }
            return savePath;
        }
    }
}

